/**
 * 
 */
package net.microlinktech.testing.ui.table;

import java.awt.Color;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;

import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.decorator.HighlighterFactory;

import com.jidesoft.grid.HierarchicalTable;
import com.jidesoft.grid.JideTable;
import com.jidesoft.grid.NestedTableHeader;
import com.jidesoft.grid.RowStripeTableStyleProvider;

/**
 * @author gonglei
 * 
 */
public class TableFactory {

    private static String getToolTip(MouseEvent e) {
        JTable table = (JTable) e.getSource();
        int row = table.rowAtPoint(e.getPoint());
        int col = table.columnAtPoint(e.getPoint());
        String tiptextString = null;
        if (row > -1 && col > -1) {
            Object value = table.getValueAt(row, col);
            if (null != value && !"".equals(value)) {
                tiptextString = value.toString();
            }
        }
        return tiptextString;
    }

    public static JXTable getGeneralConfigXTable(DefaultTableModel defaultTableModel) {
        JXTable table = new JXTable() {

            private static final long serialVersionUID = 1L;

            @Override
            public String getToolTipText(MouseEvent e) {
                return getToolTip(e);
            }

            @Override
            public Point getToolTipLocation(MouseEvent event) {
                Point point = event.getPoint();
                return new Point(point.x + 10, point.y + 10);
            }

        };

        if (defaultTableModel != null) {
            table.setModel(defaultTableModel);
        }
        table.setTableHeader(new NestedTableHeader(table.getColumnModel()));
        table.setColumnControlVisible(true);
        table.setShowGrid(true, true);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.setRowHeight(22);
        table.setFillsViewportHeight(true);
        table.addHighlighter(HighlighterFactory.createSimpleStriping());

        return table;
    }

    public static JTable getGeneralConfigTable(AbstractTableModel defaultTableModel) {
        return getGeneralConfigTable(defaultTableModel, Color.WHITE);
    }

    public static JTable getGeneralConfigTable(AbstractTableModel defaultTableModel, Color bgColor) {
        JTable table = new JTable() {
            private static final long serialVersionUID = 1L;

            @Override
            public void scrollRectToVisible(Rectangle aRect) {
                TableConfig.scrollRectToVisible(this, aRect);
            }

            @Override
            public String getToolTipText(MouseEvent e) {
                return getToolTip(e);
            }

            @Override
            public Point getToolTipLocation(MouseEvent event) {
                Point point = event.getPoint();
                return new Point(point.x + 10, point.y + 10);
            }
        };
        if (defaultTableModel != null) {
            table.setModel(defaultTableModel);
        }
        table.setShowGrid(true);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.setRowHeight(22);
        table.setBackground(bgColor);
        table.setFillsViewportHeight(true);
        table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
        table.setAutoCreateRowSorter(true);
        
//        table.setRowSelectionAllowed(true);// 设置每行可否被选择.默认为false
//    	table.setSelectionBackground(Color.white);// 设置所选择行的背景色
//    	table.setSelectionForeground(Color.red);// 设置所选择行的前景色
//    	table.setGridColor(Color.black);// 设置网格线的颜色
        return table;
    }

    public static JXTable getGeneralConfigXTable() {
        return getGeneralConfigXTable(null);
    }

    public static HierarchicalTable getGeneralConfigHierarchicalTable(DefaultTableModel defaultTableModel,
            Color bgColor) {
        HierarchicalTable hierarchicalTable = new HierarchicalTable() {

            private static final long serialVersionUID = 1L;

            @Override
            public String getToolTipText(MouseEvent e) {
                return getToolTip(e);
            }

            @Override
            public Point getToolTipLocation(MouseEvent event) {
                Point point = event.getPoint();
                return new Point(point.x + 10, point.y + 10);
            }
        };

        if (defaultTableModel != null) {
            hierarchicalTable.setModel(defaultTableModel);
        }
        hierarchicalTable.setAutoRefreshOnRowUpdate(true);
        hierarchicalTable.setTableStyleProvider(new RowStripeTableStyleProvider());
        hierarchicalTable.setBackground(bgColor);
        hierarchicalTable.setFillsViewportHeight(true);
        hierarchicalTable.setShowGrid(true);
        hierarchicalTable.setRowHeight(22);
        hierarchicalTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        return hierarchicalTable;
    }

    public static JideTable getGeneralConfigJideTable(DefaultTableModel defaultTableModel) {
        JideTable jideTable = new JideTable() {

            private static final long serialVersionUID = 1L;

            @Override
            public String getToolTipText(MouseEvent e) {
                return getToolTip(e);
            }

            @Override
            public Point getToolTipLocation(MouseEvent event) {
                Point point = event.getPoint();
                return new Point(point.x + 10, point.y + 10);
            }
        };

        if (defaultTableModel != null) {
            jideTable.setModel(defaultTableModel);
        }
        jideTable.setShowGrid(true);
        jideTable.setRowHeight(22);
        jideTable.setNestedTableHeader(true);
        jideTable.setAlwaysRequestFocusForEditor(true);
        jideTable.setClickCountToStart(2);
        jideTable.setBackground(Color.WHITE);
        jideTable.setEditorAutoCompletionMode(JideTable.EDITOR_AUTO_COMPLETION_MODE_TABLE);
        jideTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jideTable.setFillsViewportHeight(true);
        jideTable.setAutoCreateRowSorter(true);

        return jideTable;
    }

//    public static PropertyTable getGeneralConfigPropertyTable(
//            PropertyTableModel<BasicProperty> propertyTableModel) {
//        PropertyTable propertyTable = new PropertyTable();
//
//        if (propertyTableModel != null) {
//            propertyTable.setModel(propertyTableModel);
//        }
//        propertyTable.setRowHeight(24);
//        propertyTable.setRowAutoResizes(false);
//        propertyTable.expandFirstLevel();
//        propertyTable.setTableStyleProvider(new RowStripeTableStyleProvider());
//        propertyTable.setPaintMarginBackground(true);
//        propertyTable.setDoubleClickEnabled(true);
//
//        return propertyTable;
//    }

}